﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Hutech.Authentication.Library.Web.Framework;
using Resources;
using Hutech.Authentication.Library.Business.WebHelpers;
using log4net;
using Hutech.Authentication.Library.Business;

namespace Hutech.CRM.Web.Controls.ManageRoleAndModule
{
    public partial class PopupAddUser : System.Web.UI.UserControl
    {
        #region Cac su kien uy thac
        //Tao su kien uy thac
        public event EventHandler ReloadData;
        protected void onReloadData()
        {
            if (ReloadData != null)
            {
                ReloadData(this, new EventArgs());
            }
        }
        #endregion


        //Su kien xay ra dau tien... khai bao cac su kien dc dung trong popup
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            //LinkButtonCancel.Click += new EventHandler(LinkButtonCancel_Click);
            rptUsers.ItemCommand += new RepeaterCommandEventHandler(rptUsers_ItemCommand);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            populateText();
        }

        #region Show All information on popup
        public void Show(Guid RoleID)
        {
            ModalPopupAddUser.Show(); //Hien thi popup
            _RoleID.Value = RoleID.ToString();
            BindListForAdmin(RoleID);
        }

        protected void populateText()
        {
            lblHead.Text = "Thêm quyền người dùng";
        }

        private void BindListForAdmin(Guid RoleID)
        {
            rptUsers.DataSource = Role.GetUsersNotInRole(RoleID);
            rptUsers.DataBind();
            this.pnlEditTTUpdate.Update();// chu y cai nay
        }
        #endregion

        #region All Event
        //Xu ly: Kiem tra quyen cua user khi click.
        //Neu user co quyen UnAuthenticated. ---> Xoa quyen hien tai sau do them moi. Luc nay user chi co 1 quyen
        void rptUsers_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Chon":
                    Guid _UserID = Guid.Parse(e.CommandArgument.ToString());
                    if (Role.CheckRoleAuthenticatedByUserID(_UserID))
                        Role.DeleteUserRoles(_UserID);
                    Role.AddUser(Guid.Parse(_RoleID.Value), _UserID);
                    //Hide();
                    ModalPopupAddUser.Hide();
                    onReloadData();
                    break;
            }
        }
        #endregion
    }
}
